﻿using B.S.InOut.Data;
using B.S.InOut.Domain;
using B.S.InOut.Read.API.Application.Command;
using MediatR;
using static B.S.InOut.Read.API.Application.Command.GetProductsByConditionsCommand;
using static B.S.InOut.Read.API.Application.Command.GetProductsByConditionsCommand.DeleteProductsCommand;

namespace B.S.InOut.Read.API.Application.CommandHandler
{
    // 插入产品命令处理程序类，实现 IRequestHandler 接口，处理插入产品命令
    public class InsertProductCommandHandler : IRequestHandler<InsertProductCommand, int>
    {
        // 产品仓储实例
        private readonly IProductRepository _repository;

        // 构造函数，注入产品仓储实例
        public InsertProductCommandHandler(IProductRepository repository)
        {
            _repository = repository;
        }

        // 处理插入产品命令的异步方法
        public async Task<int> Handle(InsertProductCommand request, CancellationToken cancellationToken)
        {
            // 调用仓储的插入方法
            return await _repository.InsertProductAsync(request.Product);
        }
    }

    // 根据 ID 获取产品命令处理程序类，实现 IRequestHandler 接口，处理获取产品命令
    public class GetProductByIdCommandHandler : IRequestHandler<GetProductByIdCommand, ProductModel>
    {
        // 产品仓储实例
        private readonly IProductRepository _repository;

        // 构造函数，注入产品仓储实例
        public GetProductByIdCommandHandler(IProductRepository repository)
        {
            _repository = repository;
        }

        // 处理获取产品命令的异步方法
        public async Task<ProductModel> Handle(GetProductByIdCommand request, CancellationToken cancellationToken)
        {
            // 调用仓储的获取单个产品方法
            return await _repository.GetProductByIdAsync(request.Id);
        }
    }

    // 获取所有产品命令处理程序类，实现 IRequestHandler 接口，处理获取所有产品命令
    public class GetAllProductsCommandHandler : IRequestHandler<GetAllProductsCommand, IEnumerable<ProductModel>>
    {
        // 产品仓储实例
        private readonly IProductRepository _repository;

        // 构造函数，注入产品仓储实例
        public GetAllProductsCommandHandler(IProductRepository repository)
        {
            _repository = repository;
        }

        // 处理获取所有产品命令的异步方法
        public async Task<IEnumerable<ProductModel>> Handle(GetAllProductsCommand request, CancellationToken cancellationToken)
        {
            // 调用仓储的获取所有产品方法
            return await _repository.GetAllProductsAsync();
        }
    }

    // 更新产品命令处理程序类，实现 IRequestHandler 接口，处理更新产品命令
    public class UpdateProductCommandHandler : IRequestHandler<UpdateProductCommand, int>
    {
        // 产品仓储实例
        private readonly IProductRepository _repository;

        // 构造函数，注入产品仓储实例
        public UpdateProductCommandHandler(IProductRepository repository)
        {
            _repository = repository;
        }

        // 处理更新产品命令的异步方法
        public async Task<int> Handle(UpdateProductCommand request, CancellationToken cancellationToken)
        {
            // 调用仓储的更新方法
            return await _repository.UpdateProductAsync(request.Product);
        }
    }

    // 根据 ID 删除产品命令处理程序类，实现 IRequestHandler 接口，处理删除产品命令
    public class DeleteProductCommandHandler : IRequestHandler<DeleteProductCommand, int>
    {
        // 产品仓储实例
        private readonly IProductRepository _repository;

        // 构造函数，注入产品仓储实例
        public DeleteProductCommandHandler(IProductRepository repository)
        {
            _repository = repository;
        }

        // 处理删除产品命令的异步方法
        public async Task<int> Handle(DeleteProductCommand request, CancellationToken cancellationToken)
        {
            // 调用仓储的删除方法
            return await _repository.DeleteProductAsync(request.Id);
        }

        /// <summary>
        /// 多条件分页查询产品命令处理程序
        /// </summary>
        public class GetProductsByConditionsCommandHandler : IRequestHandler<GetProductsByConditionsCommand, IEnumerable<ProductModel>>
        {
            private readonly IProductRepository _repository;

            /// <summary>
            /// 构造函数，注入产品仓储实例
            /// </summary>
            /// <param name="repository">产品仓储实例</param>
            public GetProductsByConditionsCommandHandler(IProductRepository repository)
            {
                _repository = repository;
            }

            /// <summary>
            /// 处理多条件分页查询产品命令的异步方法
            /// </summary>
            /// <param name="request">多条件分页查询产品命令</param>
            /// <param name="cancellationToken">取消令牌</param>
            /// <returns>分页后的产品列表</returns>
            public async Task<IEnumerable<ProductModel>> Handle(GetProductsByConditionsCommand request, CancellationToken cancellationToken)
            {
                return await _repository.GetProductsByConditionsAsync(request.ProductNum, request.ProductName, request.Category, request.pageindex, request.pageSize);
            }
            /// <summary>
            /// 批量删除产品命令处理程序
            /// </summary>
            public class DeleteProductsCommandHandler : IRequestHandler<DeleteProductsCommand, int>
            {
                private readonly IProductRepository _repository;

                /// <summary>
                /// 构造函数，注入产品仓储实例
                /// </summary>
                /// <param name="repository">产品仓储实例</param>
                public DeleteProductsCommandHandler(IProductRepository repository)
                {
                    _repository = repository;
                }

                /// <summary>
                /// 处理批量删除产品命令的异步方法
                /// </summary>
                /// <param name="request">批量删除产品命令</param>
                /// <param name="cancellationToken">取消令牌</param>
                /// <returns>受影响的行数</returns>
                public async Task<int> Handle(DeleteProductsCommand request, CancellationToken cancellationToken)
                {
                    return await _repository.DeleteProductsAsync(request.ProductIds);
                }
            }
        }
    }
    /// <summary>
    /// 处理批量查询产品数据命令的处理程序
    /// </summary>
    public class GetProductModelsByIdsCommandHandler : IRequestHandler<GetProductModelsByIdsCommand, IEnumerable<ProductModel>>
    {
        private readonly IProductRepository _repository;

        public GetProductModelsByIdsCommandHandler(IProductRepository repository)
        {
            _repository = repository;
        }

        public async Task<IEnumerable<ProductModel>> Handle(GetProductModelsByIdsCommand request, CancellationToken cancellationToken)
        {
            return await _repository.GetProductModelsByIdsAsync(request.ProductIds);
        }
    }
}
